package com.lingx.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.lingx.bean.TokenBean;

public class TokenUtils {
	//过期时间设置(30分钟)
    private static final long EXPIRE_TIME = 30*60*1000;

    //私钥设置
    private static final String TOKEN_SECRET = Utils.getRandomString(32);
    
    public static void main(String args[]) {
    	TokenBean bean=new TokenBean();
    	bean.setLastLoginTime(System.currentTimeMillis());
    	bean.setOrgIds("12312312312321313213213213123131");
    	bean.setRoleIds("321123333311111111111113333333213123");
    	bean.setUserId("43251233333333333333333333333333333333333333");
    	//bean.setUserName("345345");
    	String token=getToken(bean);
    	System.out.println(token);
    	TokenBean bean1=getTokenData(token);
    	System.out.println(bean1.getOrgId());
    }

    public static String getToken(TokenBean token){

        //过期时间和加密算法设置
        Date date=new Date(System.currentTimeMillis()+EXPIRE_TIME);
        Algorithm algorithm =Algorithm.HMAC256(TOKEN_SECRET);

        //头部信息
        Map<String,Object> header=new HashMap<>(2);
        header.put("typ","JWT");
        header.put("alg","HS256");

        return JWT.create()
                .withHeader(header)
                .withClaim("userId",token.getUserId())
                .withClaim("orgIds",token.getOrgIds())
                .withClaim("roleIds",token.getRoleIds())
                .withClaim("lastLoginTime",token.getLastLoginTime())
                .withExpiresAt(date)
                .sign(algorithm);

    }

    public static TokenBean getTokenData(String token){
    	//return  JWT.decode(token).getClaim("userId").asString();
        DecodedJWT jwt = JWT.decode(token);
        TokenBean tk = new TokenBean();

        tk.setUserId(jwt.getClaim("userId").asString());
        tk.setOrgIds(jwt.getClaim("orgIds").asString());
        tk.setRoleIds(jwt.getClaim("roleIds").asString());
        tk.setLastLoginTime(jwt.getClaim("lastLoginTime").asLong());

        return tk;
    }

    public static String creatToken(String userId,String userName,String orgIds,String roleIds){
        //这里是传入的是token对象，决定token的内容
    	TokenBean tk=new TokenBean();
    	tk.setUserId(userId);
    	tk.setUserName(userName);
    	tk.setOrgIds(orgIds);
    	tk.setRoleIds(roleIds);
    	tk.setLastLoginTime(System.currentTimeMillis());
		//交给上面的实现类得到token
        return getToken(tk);
    }

    public static boolean verifyToken(String token) {
    	try {
			Algorithm algorithm =Algorithm.HMAC256(TOKEN_SECRET);
			JWT.require(algorithm).build().verify(token).getClaims();
			return true;
		} catch (Exception e) {
			//e.printStackTrace();
			return false;
		}
    }
    
    
    public static String getTokenDataUserId(String token){
        return JWT.decode(token).getClaim("userId").asString();
    }
}
